var ShaderUtils = require("ShaderUtils");

cc.Class({
    extends: cc.Component,

    properties: {
       sp: cc.Sprite,
       rtSp: cc.Sprite,
       frameCount : 0,
       size : cc.size,
    //    targetSp: cc.Sprite,
    
    },

    // use this for initialization
    onLoad: function () {
        
        this._angle = 0;
        this._radius = 0.1;
        // ShaderUtils.setShader(this.rtSp, "wave"); 
        ShaderUtils.setShader(this.sp, "wave"); 
        this.size = new cc.size();
        this.resize();
        cc.view.resizeWithBrowserSize(true);
        var policy = cc.ResolutionPolicy.FIXED_HEIGHT;//new cc.ResolutionPolicy(cc.ContainerStrategy.EQUAL_TO_FRAME, cc.ContentStrategy.EXACT_FIT);
        cc.view.setDesignResolutionSize(1024, 768, policy);
    },

    resize: function(){
        var _size = cc.director.getWinSize();
        if(_size.width !== this.size.width || _size.height !== this.size.height){
            this.size = _size;
            this.rtSp.node.width = this.size.width;
            this.rtSp.node.height = this.size.height;
            // console.log("width: " + this.rtSp.node.width + ", height: " +this.rtSp.node.height);
            this.sp.node.x = this.size.width / 2.0;
            this.sp.node.y = this.size.height / 2.0
            this.rtSp.node.x = this.size.width / 2.0;
            this.rtSp.node.y = this.size.height / 2.0;
            this.rt = cc.RenderTexture.create(this.size.width, this.size.height);
        }
        
    },


    screenShoot: function(){

        this.resize();

        this.rtSp.node.active = false;
        // 创建 renderTexture
        var renderTexture = this.rt;
        
        //实际截屏的代码
        renderTexture.begin();
        //this.richText.node 是我们要截图的节点，如果要截整个屏幕，可以把 this.richText 换成 Canvas 切点即可
        // this.cocos._sgNode.visit();

        cc.director.mainLoop();
        // cc.renderer.rendering(cc._renderContext);


        renderTexture.end();

        // 获取SpriteFrame
        var nowFrame = renderTexture.getSprite().getSpriteFrame();

        // 赋值给需要截图的精灵
        this.rtSp.spriteFrame = nowFrame;
        this.rtSp.node.scaleY = -1;
        this.rtSp.node.active = true;
    },

    // called every frame
    update: function (dt) {
        // if(this._radius < 0.8){
        //     this._radius += 0.001;
        //     this._angle += 0.005;
        //     ShaderUtils.update(this._angle, this._radius);
        // }
        this.frameCount++;
        if(this.frameCount % 2 === 0)
        {
            this.screenShoot();
        }

        this.sp.node.rotation += 0.05;

        this._radius += 0.016;
        ShaderUtils.updateWave2D(this._radius, 35.0, 10.0, this.size);
        

        
        // this.renderTexture.beginWithClear(0, 0, 0, 255*0.9);
        // this.sp._sgNode.visit();
        // this.renderTexture.end()
        // this.targetSp.spriteFrame._texture = this.renderTexture._texture;

    },
});
